package com.example.demo.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.Interface.DataRepo;
import com.example.demo.entity.Data;
import com.example.demo.service.HttpClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.annotation.Resources;

@RestController
public class GetData {
    @Autowired
    HttpClient httpClient;

    @Resource
    DataRepo dataRepo;

    @RequestMapping(value="/getdata")
    public void getData(){
        //String url = "http://192.168.86.114/data";
        String url = "http://192.168.43.56/data";
        HttpMethod httpMethod = HttpMethod.GET;
        MultiValueMap<String,String> params = new LinkedMultiValueMap<>();

        String json = httpClient.Client(url,httpMethod,params);
        System.out.println(json);

        //String字符串转换为Json对象
        JSONObject jobj = JSON.parseObject(json);
        JSONObject info = jobj.getJSONObject("info");

        //遍历每一个json对象，将内容存放到DATA对象中
        String wendu = jobj.getString("wendu");
        String shidu = jobj.getString("shidu");

        String ip  = info.getString("ip");
        String time = info.getString("time");
        String equipment = info.getString("equipment");

        Data data = new Data();
        data.setIp(ip);
        data.setShidu(shidu);
        data.setEquipment(equipment);
        data.setTime(time);
        data.setWendu(wendu);

        dataRepo.save(data);


    }

}
